home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3579 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.7 KB

  1. Path: res.com!usenet
  2. From: danlynes@res.com
  3. Newsgroups: comp.lang.c
  4. Subject: Re: New C Programmer Has A Problem
  5. Date: 30 Jan 1996 02:50:50 GMT
  6. Organization: RES Online
  7. Message-ID: <4ek12b$1li@clare.res.com>
  8. References: <4ehpa3$6kl@nntp.novia.net>
  9. Reply-To: danlynes@res.com
  10. NNTP-Posting-Host: di017.res.com
  11. X-Newsreader: IBM NewsReader/2 v1.2.5
  12.  
  13. In <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net (Tony Syslo) writes:
  14.  
  15. >  I have a question on C... why isn't this program working?!
  16. >  
  17. >  Program START:
  18. >  /* File Open */
  19. >  
  20. >  #include <clib/dos_protos.h>
  21.  
  22. First off, this is not ANSI C...if you want an intelligent reply, try recoding
  23. it using ANSI C, or at least K&R C.
  24.  
  25. >  #include <dos/dos.h>
  26. >  #include <stdio.h>
  27. >  #include <exec/types.h>
  28.  
  29. The above is too wierd. :)
  30.  
  31. >  void main();
  32. >  
  33. >    char *name;
  34. >    int age;
  35. >            
  36. >  void main()
  37. >  
  38. >  {
  39. >    struct FileHandle *file_handle;
  40.  
  41. Change above to:
  42.  
  43.     FILE *fpFile ;
  44.  
  45. >    long bytes_written;
  46. >    long bytes_read;
  47.  
  48.     int  BytesWritten ;
  49.     int  BytesRead ;
  50.  
  51. >   printf("Enter your name: ");
  52. >   scanf("%s",name);
  53.  
  54. How can you read data into an uninitialized pointer?  You need to either
  55. dynamically allocate storage for the variable, or malloc it.
  56.  
  57. i.e.  After you declare your variables, try doing the following:
  58.  
  59. name = ( unsigned char * )malloc( 80 ) ;
  60.  
  61. This will allocate 80 bytes for the string (79 plus a null character).
  62.  
  63. >   printf("\nEnter your age: ");  scanf("%d",age);
  64.  
  65. Keep it one function per line.  i.e.:
  66.  
  67. printf( :\nEnter your age: ") ;
  68. scanf( "%d", age ) ;
  69.  
  70. >    file_handle=(struct FileHandle *)
  71. >      Open("RAM:You.dat",MODE_NEWFILE);
  72.  
  73. fpFile = fopen( "RAM:You.Dat", "wb" ) ;
  74.  
  75. >    /* Have we opened the file successfully? */
  76. >    if(file_handle==NULL)
  77.  
  78. if( !( fpFile ) )
  79.  
  80. >    {
  81. >      printf("Could not open the file!\n");
  82. >      Exit(0);
  83.  
  84. 'Exit(n)' is actually exit(n).  Change the case.  You should also have
  85. stdlib.h included for the prototype for the exit() function.
  86.  
  87. >    }
  88. >    /* We have now opened a file, and are ready to start writing: */
  89. >    bytes_written=Write(file_handle,name,sizeof(name));
  90. >    bytes_written=bytes_written+Write(file_handle,age,sizeof(age));
  91.  
  92. You cannot get sizeof( name ), as name is an uninitialize pointer.  Please
  93. see my reference to malloc above.  As I used malloc, we will not use sizeof,
  94. as you cannot get a sizeof of a generic pointer...only a struct, union, or
  95. dynamically allocated array.
  96.  
  97. BytesWritten = fwrite( name, 80, 1, fpFile ) ;
  98. BytesWritten = fwrite( &age, sizeof( age ), 1, fpFile ) ;
  99.  
  100. >  
  101. >    if(bytes_written!=sizeof(name)+sizeof(age))
  102.  
  103. if( BytesWritten != sizeof( age )+80 )
  104.  
  105. >    {
  106. >      printf("Could not save the list!\n");
  107. >      Close(file_handle);
  108.  
  109. fclose( fpFile ) ;
  110. free( name ) ;
  111.  
  112. >      Exit(0);
  113.  
  114. exit( 0 ) ;
  115.  
  116. >    }
  117. >    else
  118. >      printf("Saved successfully!\n");
  119. >    printf("Memory cleared!\n");
  120. >     name="";
  121.  
  122. strcpy( name, "" ) ;
  123.  
  124. >     age=0;
  125. >    printf("Loading!\n");
  126. >    Seek(file_handle,0,OFFSET_BEGINNING);
  127.  
  128. fseek( fpFile, 0L, SEEK_SET ) ;
  129.  
  130. >    bytes_read=Read(file_handle,name,sizeof(name));
  131. >    bytes_read=bytes_read+Read(file_handle,age,sizeof(age));
  132.  
  133. BytesRead = fread( name, 80, 1, fpFile ) ;
  134. BytesRead = fread( &age, sizeof( age ), 1, fpFile ) ;
  135.  
  136. >    if(bytes_written!=sizeof(name)+sizeof(age))
  137.  
  138. Shouldn't the above be 'bytes_read'?
  139.  
  140. >    {
  141. >      printf("Could not read the list!\n");
  142. >      Close(file_handle);
  143. >      Exit(0);
  144.  
  145. fclose( fpFile ) ;
  146. free( name ) ;
  147. exit( 0 ) ;
  148.  
  149. >    }
  150. >    /* Print out the data: */
  151. >      printf("Hello, %s!\",name);
  152. >      printf("You are %d years old!\n",age);
  153. >    /* Close the file: */
  154. >    Close(file_handle);
  155.  
  156. fclose( fpFile ) ;
  157. free( name ) ;
  158.  
  159. >  }
  160. >  
  161. >  Program END:
  162. >  
  163. >  Any help if MUCH appreciated...
  164. > I am using the SAS/C 6.50 compiler...
  165. >       
  166. >  Also, is there any of you whom might have a routine to figure up random (or
  167. >  even psuedo-random) numbers?? Some algorithm, or such, as I need one BADLY!!
  168.  
  169. Your compiler should have one.  Reference the srand() function, located in
  170. your stdlib.h header.
  171.  
  172. BTW, I'm curious.  What operating system is the above compiler for?  Is it
  173. for MS-DOS?  It doesn't seem to conform to any standards, whatsoever.  Or,
  174. perhaps you could be misinterpreting your manual.
  175.  
  176. ttfn.
  177.  
  178. +------------------------------------------------------+
  179. !  OS/2         - The Champion of Operating Systems    !
  180. !  Enitharmon/2 - A Powerful BBS Package               !
  181. !  What a great pair!               danlynes@res.com   !
  182. !  www.res.com/~danlynes/index.html                    !
  183. !  www.res.com/~danlynes/Enitharmon!2/index.html       !
  184. !  The Enitharmon Beta project is now under way.       !
  185. !  To subscribe to the mailing list, send e-mail to    !
  186. !  danlynes@res.com or daniel.lynes@ideasnet.com       !
  187. +------------------------------------------------------+
  188.  
  189.